#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest 
@File ：recycle_bin.py
@Author ：admin
@Date ：2024/3/20 14:42 
"""
from kuberCloudTest.cloud_resources.urls import CloudResourcesUrls
from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getOperationType

_VM_RECYCLE_RESOURCE = 1
_DISK_RECYCLE_RESOURCE = 0


class recycleBinUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def _getRecycleResource(accessToken, resourceType, searchType, keyword, **kwargs):
    """
    get recycle resource
    :param accessToken: access token
    :param resourceType: 0:vm; 1:disk
    :param searchType:
    :param keyword:
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'keyword': keyword,
              'state': 'deleted'}
    if resourceType == _DISK_RECYCLE_RESOURCE:
        uri = CloudResourcesUrls.getRecycleDisk.value
        params['searchitem'] = searchType
    else:
        uri = CloudResourcesUrls.getRecycleVm.value
        params['searchtype'] = searchType
    recycle_bin_utils = recycleBinUtils(accessToken=accessToken)
    return recycle_bin_utils.cloud_http_request.get(uri, params)


def getRecycleVm(accessToken, **kwargs):
    """
    get recycle vm
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   searchType: 0: name; 2: physical machine uuid
    :return:
    """
    searchType = kwargs.get('searchType', 0)
    keyword = kwargs.get('keyword', '')
    return _getRecycleResource(accessToken,
                               _VM_RECYCLE_RESOURCE,
                               searchType,
                               keyword,
                               offset=kwargs.get('offset', OFFSET),
                               limit=kwargs.get('limit', LIMIT))


def getRecycleDisk(accessToken, **kwargs):
    """
    get recycle disk
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   searchType: 0: name; 1: disk uuid; 3: physical machine uuid
    :return:
    """
    searchType = kwargs.get('searchType', 0)
    keyword = kwargs.get('keyword', '')
    return _getRecycleResource(accessToken,
                               _DISK_RECYCLE_RESOURCE,
                               searchType,
                               keyword,
                               offset=kwargs.get('offset', OFFSET),
                               limit=kwargs.get('limit', LIMIT))


def deleteRecycleVm(accessToken, vmIds):
    """
    彻底删除云主机
    :param vmIds: 回收站云主机UUID(多个使用英文逗号分割)
    :param accessToken: access token
    :return:
    """
    operationType = 1010
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['ids'] = vmIds
    recycle_bin_utils = recycleBinUtils(accessToken=accessToken)
    return recycle_bin_utils.cloud_http_request.post(CloudResourcesUrls.deleteRecycleVm.value, data=data)


def deleteRecycleDisk(accessToken, diskIds):
    """
    彻底删除云硬盘
    :param diskIds: 回收站云硬盘UUID(多个使用英文逗号分割)
    :param accessToken: access token
    :return:
    """
    operationType = 1911
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['ids'] = diskIds
    recycle_bin_utils = recycleBinUtils(accessToken=accessToken)
    return recycle_bin_utils.cloud_http_request.post(CloudResourcesUrls.deleteRecycleDisk.value, data=data)
